﻿using System.Data.Entity.ModelConfiguration;
using PizzaMario.Domain.Entities;
using System.ComponentModel.DataAnnotations.Schema;

namespace PizzaMario.Domain.Mapping
{
    public class PizzaMap : EntityTypeConfiguration<Pizza>
    {
        public PizzaMap()
        {
            //Sets primary key configuration
            HasKey(t => t.PizzaId);

            //Properties
            Property(t => t.Name).HasMaxLength(100);

            //Configures table name where entity is mapped to
            ToTable("Pizzas");

            //Interesting stuff here :D
            //We say that Pizza has many Toppings, and each Topping has many pizza's(many to many relationship :D).
            //We then go and say that the relation table should be named PizzaToppingMapping
            //The left key (from a pizzas perspective, so the pizza key) should be named PizzaID and the right key ToppingId
            //Now we have a table that we can control and is not directly mapped to a class.
            HasMany(t => t.Toppings).WithMany(t => t.Pizzas).Map(m =>
                                                                   {
                                                                       m.ToTable("PizzaToppingMapping");
                                                                       m.MapLeftKey("PizzaId");
                                                                       m.MapRightKey("ToppingId");
                                                                   });
        }
    }
}
